// Fill out your copyright notice in the Description page of Project Settings.

#include "EmptyLandGameMode.h"
#include "WinstonLogger.h"
#include "PlayerCharacter.h"
#include "GameFramework/PlayerStart.h"
#include "Kismet/GameplayStatics.h"
#include "TerrainManager.h"

// 设置默认值
AEmptyLandGameMode::AEmptyLandGameMode()
{
    // 使用Winston日志记录游戏模式初始化
    FWinstonLogger::Get().Info(TEXT("EmptyLandGameMode"), TEXT("Game mode initializing"));
    
    // 设置默认Pawn类为我们的自定义角色类
    static ConstructorHelpers::FClassFinder<APawn> PlayerPawnClassFinder(TEXT("/Game/Characters/BP_PlayerCharacter"));
    if (PlayerPawnClassFinder.Class != nullptr)
    {
        DefaultPawnClass = PlayerPawnClassFinder.Class;
        FWinstonLogger::Get().Info(TEXT("EmptyLandGameMode"), TEXT("DefaultPawnClass set to Blueprint class"));
    }
    else
    {
        // 如果找不到蓝图类，使用C++类
        DefaultPawnClass = APlayerCharacter::StaticClass();
        FWinstonLogger::Get().Info(TEXT("EmptyLandGameMode"), TEXT("DefaultPawnClass set to C++ class"));
    }
    
    // 如果需要，设置玩家控制器类
    // PlayerControllerClass = AYourPlayerController::StaticClass();
    
    // 如果需要，设置HUD类
    // HUDClass = AYourHUD::StaticClass();
    
    // 记录游戏模式初始化
    FWinstonLogger::Get().Debug(TEXT("EmptyLandGameMode"), TEXT("Game mode initialized"));
}

void AEmptyLandGameMode::BeginPlay()
{
    Super::BeginPlay();

    // 初始化游戏系统
    InitializeGameSystems();
    
    // 记录游戏开始
    FWinstonLogger::Get().Info(TEXT("EmptyLandGameMode"), TEXT("Game started"));
}

APlayerController* AEmptyLandGameMode::SpawnPlayerController(ENetRole InRemoteRole, const FString& Options)
{
    APlayerController* NewController = Super::SpawnPlayerController(InRemoteRole, Options);
    
    // 记录玩家控制器生成
    FWinstonLogger::Get().Info(
        TEXT("EmptyLandGameMode"), 
        TEXT("Player controller spawned: %s"), 
        *GetNameSafe(NewController)
    );
    
    return NewController;
}

/**
 * 获取地形管理器
 * @return 地形管理器指针
 */
ATerrainManager* AEmptyLandGameMode::GetTerrainManager() const
{
    return TerrainManagerInstance;
}

/**
 * 获取相机管理器
 * @return 相机管理器指针
 */
ACameraManager* AEmptyLandGameMode::GetCameraManager() const
{
    return CameraManagerInstance;
}

void AEmptyLandGameMode::InitializeTerrainSystem()
{
    // 确保地形管理器类已设置
    if (TerrainManagerClass)
    {
        // 生成地形管理器实例
        FActorSpawnParameters SpawnParams;
        SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
        
        TerrainManagerInstance = GetWorld()->SpawnActor<ATerrainManager>(TerrainManagerClass, FVector::ZeroVector, FRotator::ZeroRotator, SpawnParams);
        
        if (TerrainManagerInstance)
        {
            // 记录地形管理器生成成功
            FWinstonLogger::Get().Info(
                TEXT("EmptyLandGameMode"), 
                TEXT("Terrain manager spawned: %s"), 
                *GetNameSafe(TerrainManagerInstance)
            );
        }
        else
        {
            // 记录地形管理器生成失败
            FWinstonLogger::Get().Error(TEXT("EmptyLandGameMode"), TEXT("Failed to spawn terrain manager"));
        }
    }
    else
    {
        // 记录地形管理器类未设置
        FWinstonLogger::Get().Warning(TEXT("EmptyLandGameMode"), TEXT("Terrain manager class not set"));
    }
}

void AEmptyLandGameMode::InitializeGameSystems()
{
    // 记录游戏系统初始化开始
    FWinstonLogger::Get().Info(TEXT("EmptyLandGameMode"), TEXT("Initializing game systems"));
    
    // 初始化地形系统
    InitializeTerrainSystem();
    
    // 初始化相机管理器
    if (CameraManagerClass)
    {
        FActorSpawnParameters SpawnParams;
        SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
        
        CameraManagerInstance = GetWorld()->SpawnActor<ACameraManager>(CameraManagerClass, FVector::ZeroVector, FRotator::ZeroRotator, SpawnParams);
        
        if (CameraManagerInstance)
        {
            FWinstonLogger::Get().Info(TEXT("EmptyLandGameMode"), TEXT("Camera manager spawned successfully"));
        }
        else
        {
            FWinstonLogger::Get().Error(TEXT("EmptyLandGameMode"), TEXT("Failed to spawn camera manager"));
        }
    }
    else
    {
        FWinstonLogger::Get().Warning(TEXT("EmptyLandGameMode"), TEXT("Camera manager class not set"));
    }
    
    // 游戏设置管理器作为游戏实例的子系统会自动初始化
    // 这里可以添加额外的初始化逻辑或验证
    if (GetWorld() && GetWorld()->GetGameInstance())
    {
        UGameSettingsManager* GameSettings = GetWorld()->GetGameInstance()->GetSubsystem<UGameSettingsManager>();
        if (GameSettings)
        {
            FWinstonLogger::Get().Info(TEXT("EmptyLandGameMode"), TEXT("Game settings manager initialized"));
        }
        else
        {
            FWinstonLogger::Get().Warning(TEXT("EmptyLandGameMode"), TEXT("Game settings manager not found"));
        }
    }
    
    // 在这里可以初始化其他游戏系统
    // 例如：天气系统、昼夜系统等
    
    // 记录游戏系统初始化完成
    FWinstonLogger::Get().Info(TEXT("EmptyLandGameMode"), TEXT("Game systems initialization completed"));
}

/**
 * 当玩家控制器已连接时调用
 * @param NewPlayer 新连接的玩家控制器
 */
void AEmptyLandGameMode::PostLogin(APlayerController* NewPlayer)
{
    Super::PostLogin(NewPlayer);
    
    // 使用Winston日志记录玩家登录信息
    FWinstonLogger::Get().Info(TEXT("EmptyLandGameMode"), TEXT("Player logged in"));
    
    // 这里可以添加玩家登录后的逻辑，如初始化玩家数据等
    // 注意：在蓝图项目中，通常会在蓝图中处理更多的游戏模式逻辑
}